package cy.security.demo.web.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: ciyuan
 * @Date: 2019/5/6 22:29
 */
//@Component
@Aspect
@Slf4j
public class TimeAspect {

    @Around("execution(* cy.security.demo.web.controller.*.*(..))")
    public Object HandlerControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("time aspect start");
        Object[] args = pjp.getArgs();
        for (Object arg : args) {
            System.out.println("arg: " + arg);
        }

        long start = System.currentTimeMillis();
        Object o = pjp.proceed();

        System.out.println("time aspect 耗时 ： " + (System.currentTimeMillis() - start));
        System.out.println("time aspect end");

        return o;
    }

    @Before("execution(public * cy.security.demo.web.controller.*.*(..))")
    public Object before(JoinPoint pjp) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("URL : ")
                .append(request.getRequestURL().toString())
                .append("\t")
                .append("请求方式:").append(request.getMethod())
                .append("\t")
                .append("IP : ").append(request.getRemoteAddr());
        // 记录下请求内容
        log.info(stringBuffer.toString());
        try {
            Object[] args = pjp.getArgs();
            Signature signature = pjp.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            String[] strings = methodSignature.getParameterNames();
            stringBuffer.setLength(0);
            stringBuffer.append("入参 ");
            for (int i = 0; i < strings.length; i++) {
                stringBuffer.append(strings[i]).append(" = ").append(args[i]).append("\t");
            }
            log.info(stringBuffer.toString());
            return pjp;
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }

    @AfterReturning(returning = "pjp", pointcut = "execution(public * cy.security.demo.web.controller.*.*(..))")
    public void afterMethod(Object pjp) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("URL : ")
                .append(request.getRequestURL().toString())
                .append("\t")
                .append("请求方式:").append(request.getMethod())
                .append("\t")
                .append("IP : ").append(request.getRemoteAddr());
        // 记录下请求内容
        log.info(stringBuffer.toString());
        if (pjp != null) {
            log.info("返回值 " + pjp.toString());
        }
    }

}
